<template>
  <div class="EFormItem">
    <el-row>
      <el-col :span="24">
        <dynForm :info-form="infoForm" :search-tags="searchTags" :select-list="selectList">
          <el-button
            type="primary"
            icon="el-icon-search"
            @click="doSearch"
          >
            {{ $t("search") }}</el-button
            >
        </dynForm>
      </el-col>
    </el-row>
    <div class="table-buttons">
      <el-button
        v-btnPermission:add
        type="primary"
        icon="el-icon-plus"
        @click="newAdd">{{
          $t("add")
        }}</el-button>
      <el-button
        v-btnPermission:edit
        :disabled="UpdateDisabled"
        type="primary"
        icon="el-icon-edit"
        @click="modify">{{
          $t("modify")
        }}</el-button>
      <el-button
        v-btnPermission:delete
        :disabled="DeleteDisabled"
        type="danger"
        icon="el-icon-delete"
        @click="doDelete">{{
          $t("delete")
        }}</el-button>
    </div>
    <!-- 表单部分 -->
    <el-card class="box-card" shadow="never">
      <el-row>
        <dynTable
          ref="dynTable"
          :loading="tableLoading"
          :table-data="tableData"
          :table-columns="tableColumns"
          :handle-selection-change="handleSelectionChange"
          :row-click="rowClick"
        />
      </el-row>
      <!--分页组件-->
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :size.sync="listQuery.size"
        @pagination="getList"
      />
    </el-card>
    <!-- 新增弹窗组件 -->
    <addForm ref="addForm" @initList="initList"/>
    <!-- 编辑弹窗组件 -->
    <modifyForm ref="modifyForm" @initList="initList"/>
  </div>
</template>
<script>
import mySetting from '@/mixins/mySetting'
import eFormCheckApi from '@/api/eForm/eFormCheck'
import addForm from './components/addForm'
import modifyForm from './components/modifyForm'
import networkTips from '@/mixins/networkTips'
import { clearNull } from '@/utils/myTools'
export default {
  name: 'EFormItem',
  components: { addForm, modifyForm },
  mixins: [mySetting, networkTips],
  data() {
    return {
      infoForm: {},
      searchTags: [
        { label: this.$t('FormName'), prop: 'formName', tag: 'DynInput' },
        { label: this.$t('formPrefix'), prop: 'formPrefix', tag: 'DynInput' },
        { label: this.$t('projectCategory'), prop: 'category', tag: 'DynSelect', translate: true, event: async(value) => {
          if (value) {
            const res = await eFormCheckApi.getFormTypeList({ category: value })
            this.$set(this.infoForm, 'formType', '')
            this.$set(this.selectList, 'formType', res.data)
          } else {
            this.$set(this.infoForm, 'formType', '')
            this.$set(this.selectList, 'formType', [])
          }
        } },
        { label: this.$t('formType'), prop: 'formType', tag: 'DynSelect', translate: true }
      ],
      selectList: {},
      multipleSelection: [],
      tableColumns: [
        { prop: '', label: '', type: 'selection' },
        { prop: '', label: this.$t('index'), type: 'index', width: '50px', templete: (_scope) => {
          return (
            <span>{ (this.listQuery.page - 1) * this.listQuery.size + _scope.$index + 1 }</span>
          )
        } },
        { prop: 'formName', label: this.$t('FormName') },
        { prop: 'formType', label: this.$t('formType'), templete: (_scope) => {
          return (
            <span>{ this.$t(_scope.row.formType) }</span>
          )
        } },
        { prop: '', label: '子表单类型' },
        { prop: 'formPrefix', label: this.$t('formPrefix') },
        { prop: 'status', label: this.$t('iseffective'), templete: (_scope) => {
          return (
            <el-switch
              v-model={_scope.row.status}
              active-value={1}
              inactive-value={0}
              active-color='#13ce66'
              inactive-color='#ff4949'
              disabled/>
          )
        } },
        { prop: 'version', label: this.$t('formVersion') },
        { prop: 'remark', label: this.$t('remark') }
      ],
      tableLoading: true,
      UpdateDisabled: true,
      DeleteDisabled: true
    }
  },
  created() {
    this.initList()
    this.getCategoryList()
  },
  methods: {
    async initList() {
      const res = await eFormCheckApi.getList({ ...clearNull(this.infoForm), ...this.listQuery })
      this.tableData = res.data
      this.total = res.total
      this.tableLoading = false
    },
    doSearch() {
      this.listQuery.page = 1
      this.initList()
    },
    // 删除
    doDelete() {
      this.$confirm(
        `${this.$t('confirmDelete')}${this.multipleSelection.length}${this.$t(
          'recordCount'
        )}?`,
        this.$t('tip'),
        {
          confirmButtonText: this.$t('confirm'),
          cancelButtonText: this.$t('cancel'),
          type: 'warning'
        }
      ).then(() => {
        const ids = this.multipleSelection.map(ele => ele.id)
        eFormCheckApi.doDelete({ ids }).then(res => {
          this.netTips(res, this.setOnePage, this.initList)
        })
      })
    },
    // 新增
    newAdd() {
      this.$refs.addForm.openDialog()
    },
    // 编辑
    async modify() {
      const res = await eFormCheckApi.getEditForm({ formId: this.multipleSelection[0].id })
      this.$refs.modifyForm.openDialog(res.data)
    },
    async getFormTypeList(item, row) {
      if (item.change && row) {
        const res = await eFormCheckApi.getFormTypeList({ category: this.infoForm.category })
        this.$set(this.infoForm, 'formType', '')
        this.$set(this.selectList, 'formType', res.data)
      }
    },
    // 详情
    scopedHandle(row) {
      this.$refs.addForm.openDialog(row.id)
    },
    // 分页
    getList(val) {
      this.listQuery = val
      this.initList()
    },
    handleSelectionChange(row) {
      this.multipleSelection = row
    },
    rowClick(row) {
      this.$refs.dynTable.$refs.multipleTable.toggleRowSelection(row)
    },
    // 项目分类
    async getCategoryList() {
      const res = await eFormCheckApi.getCategoryList({ type: 'form' })
      this.$set(this.selectList, 'category', res.data)
    }
  }
}
</script>
<style lang='scss' scoped>
@import "~@/assets/styles/defaultStyle.scss";
.EFormItem .el-row:nth-child(1){
    padding-bottom: 0;
  }
.el-form-item {
  margin: 0 10px 15px 0;
}
</style>
